***********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"********
***********************************************************************************
* This file sets up all datasets (using the datasets in the psuedo_input folder), 
* which are then saved in the folder pseudo_output. It then launches a series of
* do-files to produces the estimates in the paper. 
* We are not allowed to share all the data from the Bank of Italy, but in line 
* with the Code Sharing Policy of the Review of Financial Studies (as of July
* 2020), we include pseudo-data sets with randomly generated data to illustrate
* the format and the way the code works. See the readme file for additional 
* information.
***********************************************************************************
***********************************************************************************

**# Bookmark 1 (parameters)
global inputdirectory pseudo_input
mkdir pseudo_output
global outputdirectory pseudo_output

do 01_auxiliarydatasets

do 02_auxiliarydatasets2019_1
do 03_auxiliarydatasets2019_2
do 04_auxiliarydatasets2019_3
do 05_auxiliarydatasets2019_4
do 06_auxiliarydatasets2019_5
do 07_auxiliarydatasets2019_6
do 08_auxiliarydatasets2019_7

do 09_auxiliarydatasets2015_1
do 10_auxiliarydatasets2015_2
do 11_auxiliarydatasets2015_3
do 12_auxiliarydatasets2015_4
do 13_auxiliarydatasets2015_5
do 14_auxiliarydatasets2015_6
do 15_auxiliarydatasets2015_7

do 16_auxiliarydatasetsMONPOL_1
do 17_auxiliarydatasetsMONPOL_2
do 18_auxiliarydatasetsMONPOL_3
do 19_auxiliarydatasetsMONPOL_4

do 20_auxiliarydatasetsPLACEBO_1
do 21_auxiliarydatasetsPLACEBO_2

**# Bookmark 2 (tables and figures: text)
do TAB1
do TAB2
do TAB3
do TAB4
do TAB5
do TAB6
do TAB7
do TAB8
do TAB9

do TFIG3
do TFIG4
do TFIG5
do TFIG6
do TFIG7
do TFIG8
do TFIG9

**# Bookmark 3 (tables and figures: appendix)
do TABI
do TABIIA
do TABIIBC
do TABIIDE
do TABIIF
do TABIIG
do TABIIH
do TABIII
do TABIIIA
do TABIIIB
do TABIIIC
do TABIIID
do TABIIIE
do TABIIIG
do TABIIIH
do TABIIII
do TABIVA
do TABIVB
do TABVA
do TABVB
do TABVIIA
do TABVIIB
do TABVIIIA
do TABIXA

do TFIGIIIF

**# Bookmark 4 (convert figures to jpg)
foreach x in FIG3A FIG3B FIG4A FIG4B FIG5A1 FIG5A2 FIG5B1 FIG5B2 FIG5C1 FIG5C2 FIG5D1 FIG5D2 FIG6A FIG6B FIG7A FIG7B FIG8A FIG8B FIG9 FIGIIIFa1 FIGIIIFa2 FIGIIIFb1 FIGIIIFb2 {
graph use $outputdirectory/`x'
graph export $outputdirectory/`x'.jpg, quality(100) replace
}
graph close *

**# Bookmark 5 (convert tables to excel)
cd $outputdirectory

***************
*** Table 3 ***
***************
local i="TAB3_C1234"
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			capture outreg2 using "TAB3.xls", excel replace nocons  ctitle("") bracket bdec(3) drop(lncrefin dT1exlnassets dT2exlnassets dT3exlnassets dT4exlnassets dT5exlnassets dT7exlnassets dT8exlnassets dT9exlnassets dT10exlnassets dT11exlnassets dT12exlnassets dT1excashas dT2excashas dT3excashas dT4excashas dT5excashas dT6excashas dT7excashas dT8excashas dT9excashas dT10excashas dT11excashas)
		}
		
		if `x'>1{
			capture outreg2 using "TAB3.xls", excel append nocons  ctitle("") bracket bdec(3) drop(lncrefin dT1exlnassets dT2exlnassets dT3exlnassets dT4exlnassets dT5exlnassets dT7exlnassets dT8exlnassets dT9exlnassets dT10exlnassets dT11exlnassets dT12exlnassets dT1excashas dT2excashas dT3excashas dT4excashas dT5excashas dT6excashas dT7excashas dT8excashas dT9excashas dT10excashas dT11excashas)
		}
					
}


local i="TAB3_C5678"
foreach x in 1 2 3 4 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		outreg2 using "TAB3.xls", excel append nocons  ctitle("") bracket bdec(3) drop(lncrefin dT1exlnassets dT2exlnassets dT3exlnassets dT4exlnassets dT5exlnassets dT7exlnassets dT8exlnassets dT9exlnassets dT10exlnassets dT11exlnassets dT12exlnassets dT1excashas dT2excashas dT3excashas dT4excashas dT5excashas dT6excashas dT7excashas dT8excashas dT9excashas dT10excashas dT11excashas)
*		}
					
}		
erase "TAB3.txt"


***************
*** Table 4 ***
***************
local i="TAB4_C1234"
 
foreach x in 1 2 3 4 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			capture outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			capture outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
capture erase "`i'.txt"

***************
*** Table 5 ***
***************
local i="TAB5_C12"
 
foreach x in 1 2{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


***************
*** Table 6 ***
***************
local i="TAB6_C12"
 
foreach x in 1 2{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


***************
*** Table 7 ***
***************
local i="TAB7_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


****************
*** Table 8A ***
****************
local i="TAB8A_C612345"
 
foreach x in 2 3 4 5 6 1 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==2{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'!=2{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


****************
*** Table 8B ***
****************

local i="TAB8B_C612345"
 
foreach x in 2 3 4 5 6 1 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==2{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'!=2{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


****************
*** Table 9 ***
****************
local i="TAB9_C51234"
 
foreach x in 2 3 4 5 1 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==2{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'!=2{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"

**# Bookmark 7 (tables: appendix)

*****************
*** Table I A ***
*****************
local i="TABIA_C1a1b"
 
foreach x in 1 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"



local i="TABIA_C2a2b"
 
foreach x in 1 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*****************
*** Table I B ***
*****************
local i="TABIB_C1a1b"
 
foreach x in 1 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"



local i="TABIB_C2a2b"
 
foreach x in 1 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"



******************
*** Table II A ***
******************
local i="TABIIA_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table II B ***
******************
local i="TABIIB_C12345678"
 
foreach x in 1 2 3 4 5 6 7 8{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table II C ***
******************
local i="TABIIC_C12345678"
 
foreach x in 1 2 3 4 5 6 7 8{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table II D ***
******************
local i="TABIID_C1234567"
 
foreach x in 1 2 3 4 5 6 7{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table II E ***
******************
local i="TABIIE_C1234567"
 
foreach x in 1 2 3 4 5 6 7{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table II F ***
******************
local i="TABIIF_C12"
 
foreach x in 1 2{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table II G ***
******************
local i="TABIIG_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table II H ***
******************
local i="TABIIH"
 
foreach x in 1 {

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"

******************
*** Table II I ***
******************
local i="TABIII_C12"
 
foreach x in 1 2{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table III A ***
*******************
local i="TABIIIA_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table III B ***
*******************
local i="TABIIIB_C12"
 
foreach x in 1 2{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table III C ***
*******************
local i="TABIIIC_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table III D ***
*******************
local i="TABIIID_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table III E ***
*******************
local i="TABIIIE_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table III G ***
*******************
local i="TABIIIG_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table III H ***
*******************
local i="TABIIIH_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table III I ***
*******************
local i="TABIIII_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table IV A ***
******************
local i="TABIVA_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


******************
*** Table IV B ***
******************
local i="TABIVB_C12"
 
foreach x in 1 2{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*****************
*** Table V A ***
*****************
local i="TABVA_C12"
 
foreach x in 1 2{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"

*****************
*** Table V B ***
*****************
local i="TABVB_C12"
 
foreach x in 1 2{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


*******************
*** Table VII B ***
*******************
local i="TABVIIB_C1234"
 
foreach x in 1 2 3 4{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"


********************
*** Table VIII A ***
********************
local i="TABVIIIA_C123"
 
foreach x in 1 2 3{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"

******************
*** Table IX A ***
******************
local i="TABIXA_C123"
 
foreach x in 1 2 3{

		estimates use `i', number(`x')

		estimates store `i'`x'

		estimates replay `i'`x'

		if `x'==1{		
			outreg2 using "`i'.xls", excel replace nocons  ctitle("") bracket bdec(3) 
		}
		
		if `x'>1{
			outreg2 using "`i'.xls", excel append nocons  ctitle("") bracket bdec(3) 
		}
}
erase "`i'.txt"

cd ..
